package com.zcm.modules.zhaocai.scheduler;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import static oracle.net.aso.C01.s;

/*
 * 利用HttpClient进行post请求的工具类
 */
public class HttpClientUtil {
    public static String httpRequest(String requestUrl,String requestMethod,String outputStr){
        StringBuffer buffer=null;
        try{
            URL url=new URL(requestUrl);
            HttpURLConnection conn=(HttpURLConnection)url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod(requestMethod);
            conn.connect();
            //往服务器端写内容 也就是发起http请求需要带的参数
            if(null!=outputStr){
                OutputStream os=conn.getOutputStream();
                os.write(outputStr.getBytes("utf-8"));
                os.close();
            }

            //读取服务器端返回的内容
            InputStream is=conn.getInputStream();
            InputStreamReader isr=new InputStreamReader(is,"utf-8");
            BufferedReader br=new BufferedReader(isr);
            buffer=new StringBuffer();
            String line=null;
            while((line=br.readLine())!=null){
                buffer.append(line);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return buffer.toString();
    }

    public String doPost(String url,Map<String,String> map,String charset){
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            //设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();
            while(iterator.hasNext()){
                Entry<String,String> elem = (Entry<String, String>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
            }
            if(list.size() > 0){
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }

    public static String doGet(String url){
        //return doGet(url,"utf-8");
        return httpRequest(url,"GET",null);

    }
    public static String doGet(String url, String charset){
        HttpClient httpClient;
        HttpGet httpGet;
        String result = null;
        try{
            httpClient = new SSLClient();
            httpGet = new HttpGet(url);

            HttpResponse response = httpClient.execute(httpGet);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }
    public static void main(String[] argc) {
        System.out.println(doGet("https://wows-api.wallstreetcn.com/v2/sheet/board_stock?filter=true"));//一字板和涨停板和连板个股
        System.out.println(doGet("https://wows-api.wallstreetcn.com/v2/sheet/boom_stock"));//被砸开板
        System.out.println(doGet("https://wows-api.wallstreetcn.com/v3/saga/pool/board-down"));//跌停板
        System.out.println(doGet("https://wows-api.wallstreetcn.com/v3/saga/pool/board-preday"));//昨日涨停
        System.out.println(doGet("https://wows-api.wallstreetcn.com/v2/sheet/multi_stock"));//强势股
        System.out.println(doGet("https://forexdata.wallstreetcn.com/real?en_prod_code=000001.SS,399001.SZ,399006.SZ&fields=last_px,px_change_rate"));//上深创
        System.out.println(doGet("https://wows-api.wallstreetcn.com/statis_data/quote_change"));//涨跌平

        String s=httpRequest("https://wows-api.wallstreetcn.com/statis_data/quote_change","GET",null);
        System.out.println(s);

    }
}

